package com.example.hzzrpcclient.config.registercenter;

import com.example.hzzrpcclient.config.dto.RpcServerDto;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author ：hzz
 * @description：TODO
 * @date ：2022/1/2 22:42
 */
public class RedisRegisterCenter {

    private String keyName = "rpcserver:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public RpcServerDto searchRpcServer(String serverName){
        String s = stringRedisTemplate.opsForValue().get(keyName + serverName);
        if(!StringUtil.isNullOrEmpty(s)){
            String[] split = s.split(":");
            RpcServerDto rpcServerDto=new RpcServerDto();
            rpcServerDto.setIp(split[0]);rpcServerDto.setPort(Integer.parseInt(split[1]));
            return rpcServerDto;
        }else{
            throw new  RuntimeException(serverName+"服务未启动或者不存在");
        }
    }
}
